//
//  IntegralView.m
//  phmall
//
//  Created by admin on 2017/11/14.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "IntegralView.h"
#import "BuyPointsCell.h"

@implementation IntegralView

-(instancetype)init{
    if (self=[super init]) {
        [self addSubview:self.collectionView];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
    }];
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titles.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId=@"cell";
    BuyPointsCell *cell = (BuyPointsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.money.text=self.titles[indexPath.row];
    cell.integral.text=self.integral[indexPath.row];
    ViewBorderRadius(cell, 5, 0.5, [UIColor colorWithHexString:@"#adadad"]);
    

//    cell.backgroundColor=[UIColor redColor];
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH/3-15, 60);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 10, 10, 10);
}

//点击选定
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BuyPointsCell *cell = (BuyPointsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    ViewBorderRadius(cell, 5, 0.5, [UIColor orangeColor]);
    cell.money.textColor=[UIColor orangeColor];
    cell.integral.textColor=[UIColor orangeColor];
    NSLog(@"第%ld区，第%ld个",(long)indexPath.section,(long)indexPath.row);
    
}
//取消选定
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    BuyPointsCell *cell = (BuyPointsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    ViewBorderRadius(cell, 5, 0.5, [UIColor colorWithHexString:@"#adadad"]);
    cell.money.textColor=[UIColor blackColor];
    cell.integral.textColor=[UIColor colorWithHexString:@"#adadad"];
    NSLog(@"1第%ld区，1第%ld个",(long)indexPath.section,(long)indexPath.row);
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置collectionView滚动方向
        //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //2.初始化collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        //3.注册collectionViewCell
        //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
        [_collectionView registerClass:[BuyPointsCell class] forCellWithReuseIdentifier:@"cell"];
        
        _collectionView.showsVerticalScrollIndicator=NO;
        //4.设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
-(NSMutableArray *)titles{
    if (!_titles) {
        _titles=@[@"₱30", @"₱50",@"₱100",@"₱200",@"₱300",@"₱500",@"₱1000",@"₱3000",@"₱5000",@"₱7000",@"₱10000",@"₱11000"].copy;
    }
    return _titles;
}
-(NSMutableArray *)integral{
    if (!_integral) {
        _integral=@[@"150积分", @"250积分",@"520积分",@"1040积分",@"1560积分",@"2600积分",@"5200积分",@"15600积分",@"26000积分",@"52000积分",@"156000及积分",@"26000积分"].copy;
    }
    return _integral;
}
@end
